package com.bysj.lms.controller;


import com.bysj.lms.common.ResultModel;
import com.bysj.lms.pojo.entity.OutlineVersion;
import com.bysj.lms.service.ICourseOutlineService;
import com.bysj.lms.service.IOutlineVersionService;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xiaojiakai
 * @since 2022-04-27
 */
@RestController
@RequestMapping("/outline-version")
@Api(tags = "大纲版本管理")
public class OutlineVersionController {

    @Autowired
    IOutlineVersionService outlineVersionService;
    @Autowired
    ICourseOutlineService courseOutlineService;

    @ApiOperation("添加大纲版本信息")
    @PostMapping("/add")
    public ResultModel add(@RequestParam("outlineVersion") String ovStr, MultipartFile excel) throws IOException {
        ObjectMapper objectMapper = new ObjectMapper();
        OutlineVersion outlineVersion = objectMapper.readValue(ovStr, OutlineVersion.class);
        System.out.println(outlineVersion);
        boolean add = outlineVersionService.add(outlineVersion);
        System.out.println(outlineVersion.getNumber());
        boolean adds = courseOutlineService.adds(excel, outlineVersion.getNumber());
        return add&adds?ResultModel.success():ResultModel.failure();
    }

    @ApiOperation("查询大纲版本")
    @GetMapping("/list")
    public ResultModel list(String major){
        List<OutlineVersion> list = outlineVersionService.getList(major);
        return ResultModel.success("查询成功",list);
    }

}

